package icestorm;

import com.guo.util.ResourceUtil;
import com.slice2java.HelloPrx;
/**
 * ICE 发布topic接口
 *
 * @author keriezhang
 * @date 2016/10/31
 */
public class Publisher extends com.zeroc.Ice.Application{
    /***
     * 发布
     * @param args .
     * @return int
     */
    @Override
    public int run(String[] args)
    {
        String option = "None";
        // topicName
        String topicName = "helloService";
        int i;
        for(i = 0; i < args.length; ++i){
            String oldoption = option;
            if("--datagram".equals(args[i])){
                option = "Datagram";
            }
            else if("--twoway".equals(args[i])){
                option = "Twoway";
            }
            else if("--oneway".equals(args[i])){
                option = "Oneway";
            }
            else if(args[i].startsWith("--")){
                usage();
                return 1;
            }
            else{
                topicName = args[i++];
                break;
            }
            if(!oldoption.equals(option) && !("None").equals(oldoption)){
                usage();
                return 1;
            }
        }
        if(i != args.length){
            usage();
            return 1;
        }
        // 创建 TopicManager 代理
        com.zeroc.IceStorm.TopicManagerPrx manager = com.zeroc.IceStorm.TopicManagerPrx.checkedCast(communicator().propertyToProxy("TopicManager.Proxy"));
        if(manager == null){
            System.err.println("invalid proxy");
            return 1;
        }
        // Retrieve the topic.
        com.zeroc.IceStorm.TopicPrx topic;
        try{
            topic = manager.retrieve("helloService");
        }catch(com.zeroc.IceStorm.NoSuchTopic e){
            try{
                topic = manager.create("helloService");
            }catch(com.zeroc.IceStorm.TopicExists ex){
                System.err.println(appName() + ": temporary failure, try again.");
                return 1;
            }
        }

        //
        // Get the topic's publisher object, and create a Clock proxy with
        // the mode specified as an argument of this application.
        //
        com.zeroc.Ice.ObjectPrx publisher = topic.getPublisher();
        if("Datagram".equals(option)){
            publisher = publisher.ice_datagram();
        }else if("Twoway".equals(option)){
            // Do nothing.
        }
        else{ // if(oneway)
            publisher = publisher.ice_oneway();
        }
        HelloPrx helloService = HelloPrx.uncheckedCast(publisher);
        System.out.println("publishing tick events. Press ^C to terminate the application.");
        try{
            java.text.SimpleDateFormat date = new java.text.SimpleDateFormat("MM/dd/yy HH:mm:ss:SSS");
            int t=0;
            // 定时模拟发送数据
            while(t>=0){
                t++;
                System.out.println(t);
                helloService.sayHello(date.format(new java.util.Date()));
                try{
                    Thread.currentThread();
                    Thread.sleep(1000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        }catch(com.zeroc.Ice.CommunicatorDestroyedException ex){
            // Ignore
        }
        return 0;
    }
    private void usage(){
        System.out.println("Usage: " + appName() + " [--datagram|--twoway|--oneway] [topic]");
    }
    public static void main(String[] args){
        String pubConfigPath = ResourceUtil.getResourcePath("/iceStorm/config.pub");
        Publisher app = new Publisher();
        int status = app.main("Publisher", args, pubConfigPath);
        System.exit(status);
    }
}
